import { uploadFile } from "/utils/common";
import { chooseImage } from "/utils/common";

Component({
  mixins: [],
  data: {},
  props: {
    title: "图片上传",
    max: 99,
    multiple: false,
    required: true,
    deletable: true,
    fileList: [],
    onChange: () => { },
    onRemove: () => { }
  },
  didMount() { },
  didUpdate() { },
  didUnmount() { },
  methods: {
    onChooseImage() {
      chooseImage({
        count: this.props.multiple ? 9 : 1
      }).then(res => {
        if (res.filePaths.length) {
          uploadFile({ url: res.filePaths, type: 'image' }).then(list => {
            this.props.onChange(list);
          })
        }
      })
    },
    delImage(e) {
      dd.confirm({
        title: '提示',
        content: '您确定要删除吗？',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if (result.confirm) {
            this.props.onRemove(e.target.dataset.index);
          }
        },
      });
    },
    viewImage(e) {
      let arr = this.props.fileList.map(item => item.fileUrl)
      dd.previewImage({
        urls: arr,
        current: e.target.dataset.url
      });
    },
  },
});
